package kr.hkit.powerjava.coffee.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import kr.hkit.powerjava.coffee.db.DbUtil;
import kr.hkit.powerjava.coffee.dto.SalesRecord;

public class CoffeeDAO implements DaoInterface<SalesRecord> {

	private Connection connection;
	private PreparedStatement pstmt;

	public CoffeeDAO(Connection connection) {
		this.connection = connection;
	}

	@Override
	public void insertItem(SalesRecord item) throws SQLException {
		String sql = "insert into sales values(?, ?, ?, ?)";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item.getPdCode());
		pstmt.setInt(2, item.getPdPrice());
		pstmt.setInt(3, item.getAmount());
		pstmt.setDouble(4, item.getMarginRate());

		System.out.println(pstmt);
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	public String getName(String code) throws SQLException {
		String name = null;
		String sql = "select * from product where pdcode = ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, code);
		ResultSet rs = pstmt.executeQuery();// 결과값 포인터
		if (rs.next()) {
			name = rs.getString("pdname");
		}
		DbUtil.close(rs);
		DbUtil.close(pstmt);
		return name;
	}

	public ArrayList<String> getCodes() throws SQLException {
		ArrayList<String> codes = new ArrayList<>();
		String sql = "select pdcode from product";
		pstmt = connection.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();// 결과값 포인터
		while (rs.next()) {// 다음 레코드가 있니?
			codes.add(rs.getString("pcode"));
		}
		DbUtil.close(rs);
		DbUtil.close(pstmt);
		return codes;
	}

	@Override
	public ArrayList<SalesRecord> listItems() throws SQLException {
		ArrayList<SalesRecord> list = new ArrayList<>();
		String sql = "select p.pdcode, p.pdname, s.price, s.amount, s.marginrate from sales s, product p where s.pdcode = p.pdcode";
		pstmt = connection.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();// 결과값 포인터
		while (rs.next()) {// 다음 레코드가 있니?
			list.add(new SalesRecord(rs.getString("p.pdcode"), rs
					.getString("p.pdname"), rs.getInt("s.price"), rs
					.getInt("s.amount"), rs.getInt("s.marginrate")));
		}
		DbUtil.close(rs);
		DbUtil.close(pstmt);
		return list;
	}

	public void sortingList(boolean orderBySalesRecord) {
		ArrayList<SalesRecord> list = new ArrayList<>();
		try {
			list = listItems();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (orderBySalesRecord) {
			prnItemList(new Comparator<SalesRecord>() {

				@Override
				public int compare(SalesRecord o1, SalesRecord o2) {
					return o1.getSellingPrice() - o2.getSellingPrice();
				}
			}, list);
		} else {
			prnItemList(new Comparator<SalesRecord>() {

				@Override
				public int compare(SalesRecord o1, SalesRecord o2) {
					return o1.getMargin() - o2.getMargin();
				}
			}, list);
		}
	}

	private void prnItemList(Comparator<SalesRecord> comparator,
			ArrayList<SalesRecord> list) {
		Collections.sort(list, comparator);
		
		for (SalesRecord e : list) {
			System.out.println(e);
		}
	}
}
